const {
  mysql
} = require('../qcloud')
const {
  mysqlQuery
} = require('../tools/utils');

module.exports = async ctx => {
  let query = ctx.request.query;
  // console.log(query);
  if (query.from == 'index') {
    let sql1 = `SELECT video.* from (SELECT * from video ORDER BY counter DESC)video  LIMIT 15`;
    let sql2 = `SELECT video.* from (SELECT * from video ORDER BY create_time DESC)video  LIMIT 15`
    let sql3 = `SELECT video.* from (SELECT * from video WHERE lang='unv'  ORDER BY counter DESC )video LIMIT 15`
    let hotList = await mysqlQuery(sql1);
    let newList = await mysqlQuery(sql2);
    let uyList = await mysqlQuery(sql3);
    ctx.state.data = {
      newList,
      hotList,
      uyList,
      msg: 'success'
    }
  }
  if (query.type == 'tuijian') {
    let sql = `SELECT * from video ORDER BY RAND() LIMIT 6`
    let list = await mysqlQuery(sql)
    ctx.state.data = {
      list
    }
  }
  if (query.vid != null) {
    let id = query.vid
    await mysql('video').where('video_url', id).increment('counter', 1)
    let video = await mysql('video').select('*').where('video_url', id)
    ctx.state.data = {
      video
    }
  }
  if (query.type == 'hot') {
    let pageIndex = query.pageIndex;
    let size = 18;
    let hotList = await mysql('video')
      .select('*')
      .orderBy('counter', 'desc')
      .limit(size)
      .offset(Number(pageIndex) * size)
    ctx.state.data = {
      hotList
    }
  }
  if (query.type == 'new') {
    let pageIndex = query.pageIndex;
    let size = 18;
    let hotList = await mysql('video')
      .select('*')
      .orderBy('create_time', 'desc')
      .limit(size)
      .offset(Number(pageIndex) * size)
    ctx.state.data = {
      hotList
    }
  }
  if (query.lang) {
    let lang = query.lang;
    let sql = `SELECT video.* from (SELECT * from video WHERE lang='${lang}'  ORDER BY counter DESC )video LIMIT 15`
    let list = await mysqlQuery(sql)
    ctx.state.data = {
      list
    }
  }
};
